package com.xl.juc.ThreadPool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Executors 工具类 3大方法
 * @author Charley
 * @create 2021-01-24
 */
public class ExecutorsDemo {
    public static void main(String[] args) {
       // ExecutorService executor = Executors.newSingleThreadExecutor();//单个线程
        //ExecutorService executor = Executors.newFixedThreadPool(5);//固定的线程池大小
        ExecutorService executor = Executors.newCachedThreadPool();//可伸缩的，遇强则强，遇弱则弱

        try {
            for (int i = 1; i <=10 ; i++) {
                executor.execute(()->{
                    System.out.println(Thread.currentThread().getName()+" ok");
                });

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //线程池用完，程序结束，关闭线程池
            executor.shutdown();
        }
    }
}
